//
//  BuyerCollectionViewCell.m
//  TermOfSectionB
//
//  Created by dllo on 15/12/2.
//  Copyright © 2015年 韩震明. All rights reserved.
//

#import "BuyerCollectionViewCell.h"
#import "SYSecondTableViewCell.h"
#import "SYDesignerTableViewCell.h"
#import "SYCommodityViewController.h"
@interface BuyerCollectionViewCell ()<SYDesignerTableViewCellDelegate, SYSecondTableViewCellDelegate>

@property(nonatomic, assign)NSInteger page;
@property(nonatomic, strong)NSMutableArray *arr;
@end

@implementation BuyerCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self buyerCreateTableView];
    }
    return self;
}


- (void)buyerCreateTableView {
    
#pragma mark tableView
    self.buyerTableView = [[SYBaseTableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 113) style:UITableViewStylePlain superView:self.contentView delegate:self rowHeight:380 separatorStyle:NO showsVerticalScrollIndicator:NO showsHorizontalScrollIndicator:NO header:nil];
    
    self.page = 1;
        self.buyerTableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(header)];
        [self.buyerTableView.header beginRefreshing];
        // 上拉加载
        self.buyerTableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footer)];
    
    
    [self buyerData];
    
}

- (void)header {
    [self.arr removeAllObjects];
    [self buyerData];
}


- (void)footer {
    
    self.page++;
    NSLog(@"%ld", self.page);
    
    
        [AFN POSTDataWithURL:@"http://api.xiangqu.com/designerInfo/list" POST:[NSDictionary dictionaryWithObjectsAndKeys:@"10f0bd75af5d19089670446190e2d541", @"key", [NSString stringWithFormat:@"%ld", self.page], @"page", @"3", @"requestType", @"1", @"sequenceType", @"1446082489042", @"t", nil] block:^(id result) {
            NSMutableArray *linArr = [[NSMutableArray alloc] init];
            linArr = [CLdisgner baseModelByArr:result[@"records"]];
            for (NSInteger i = 0; i < linArr.count; i++) {
                [linArr[i] setValue:[CLdisgnerPic baseModelByArr:[linArr[i] products]] forKey:@"products"];
            }
            
            for (CLdisgner *dis in linArr) {
                [self.arr addObject:dis];
                
            }
            [self.arr removeLastObject];
            [self.buyerTableView.footer endRefreshing];
            [self.buyerTableView reloadData];
            
        }];
    
        
        
}


- (void)buyerData {
    
    [AFN POSTDataWithURL:@"http://api.xiangqu.com/designerInfo/list" POST:[NSDictionary dictionaryWithObjectsAndKeys:@"10f0bd75af5d19089670446190e2d541", @"key", @"1", @"page", @"3", @"requestType", @"1", @"sequenceType", @"1446082489042", @"t", @"705811", @"userid", nil] block:^(id result) {
        
        self.arr = [NSMutableArray array];
        self.arr = [CLdisgner baseModelByArr:result[@"records"]];
        for (NSInteger i = 0; i < self.arr.count; i++) {
            [self.arr[i] setValue:[CLdisgnerPic baseModelByArr:[self.arr[i] products]] forKey:@"products"];
        }
        
        [self.buyerTableView.header endRefreshing];
        [self.buyerTableView reloadData];
        
    }];
}

#pragma mark 协议方法, cell个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arr.count;
}

#pragma mark 协议方法, 自定义cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //    self.arr = [NSMutableArray array];
    CLdisgner *disgner = self.arr[indexPath.row];
    if ([disgner.isRecommend isEqualToString:@"1"]) {
        static NSString *reuse = @"reuse";
        SYDesignerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell = [[SYDesignerTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse];
        }
        
        cell.pushLabel.text = @"推荐";
        cell.titleLabel.text = disgner.designerName;
        [cell.phoneImage sd_setImageWithURL:[NSURL URLWithString:disgner.headPortrait]];
        cell.classifyLabel.text = disgner.designerTag;
        cell.likeLabel.text = @"相关作品";
        cell.funsLabel.text = [NSString stringWithFormat:@"作品:%@ / 粉丝:%@", disgner.productNum, disgner.fansNum];
        
        ///传数组
        cell.productArr = [self.arr[indexPath.row] products];
        [cell.picCollectionView reloadData];
        cell.tagArr = self.arr;
 
        // 往图片上按tag
        cell.phoneImage.tag = indexPath.row;
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        cell.delegate = self;
        return cell;
        
    } else {
        static NSString *reuse = @"rae";
        SYSecondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell = [[SYSecondTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse];
        }
        
        cell.pushLabel.text = disgner.designerLabel;
        cell.titleLabel.text = disgner.designerName;
        [cell.phoneImage sd_setImageWithURL:[NSURL URLWithString:disgner.headPortrait] placeholderImage:[UIImage imageNamed:@"good.png"]];
        cell.classifyLabel.text = disgner.designerTag;
        cell.likeLabel.text = @"相关作品";
        cell.funsLabel.text = [NSString stringWithFormat:@"作品:%@ / 粉丝:%@", disgner.productNum, disgner.fansNum];
        
        ///传数组
        
        cell.proArr = [self.arr[indexPath.row] products];
        [cell.picCollectionView reloadData];
        cell.bigArr = self.arr;
        cell.Delate = self;
   
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        cell.phoneImage.tag = indexPath.row;
        return cell;
    }
}

- (void)pushWhFirstId:(NSString *)fitirstId {
    
    NSLog(@"%@", fitirstId);
    
    [self.delegate push:fitirstId];
    
}

- (void)pushWithId:(NSString *)pushId {
    
    [self.delegate push:pushId];
}





@end
